package com.seed.application.service.stat;

import cn.hutool.core.util.StrUtil;
import com.seed.core.action.StatAction;
import com.seed.core.builder.datapermis.DataPermisBuilder;
import com.seed.core.builder.datapermis.DataPermisWrapper;
import com.seed.core.builder.declare.QueryDeclaredBuilder;
import com.seed.core.builder.keyword.KeywordWrapper;
import com.seed.core.builder.query.QueryBuilder;
import com.seed.core.builder.search.SearchedBuilder;
import com.seed.core.builder.search.SearchedWrapper;
import com.seed.core.pojo.seed.Seed;
import com.seed.core.pojo.seed.Stat;
import com.seed.core.query.pojo.Declared;
import com.seed.core.query.pojo.Searched;
import com.seed.util.SeedKit;

import java.util.Collections;
import java.util.List;
import java.util.Map;

public class StatService {
    private final Seed seed; // 当前模块
    private final StatAction action;

    private Map<String, String> searchParams; // 搜索参数

    private String keyword; // 关键字
    private List<List<String>> dataPermis; // 数据权限

    public StatService(Seed seed, StatAction action) {
        this.seed = seed;
        this.action = action;
    }

    public Map<String, Object> exec() {
        QueryBuilder builder = new QueryBuilder(seed);

        Map<String, Searched> searcheds = new SearchedBuilder(seed, getSearches(), searchParams).build();
        if(!searcheds.isEmpty()) builder.searchedWrapper(new SearchedWrapper(seed, searcheds));

        Map<String, Declared> declareds = new QueryDeclaredBuilder(seed, getDeclares()).build();
        if(StrUtil.isNotBlank(keyword)) builder.keywordWrapper(new KeywordWrapper(seed, declareds, keyword));

        List<Map<String, Searched>> dataPermised = new DataPermisBuilder(seed, dataPermis).build();
        if(!dataPermised.isEmpty()) builder.dataPermisWrapper(new DataPermisWrapper(seed, dataPermised));

        return new StatQuery(seed, getStats())
                .declareds(declareds)
                .searcheds(searcheds)
                .dataPermised(dataPermised)
                .keyword(keyword)
                .query();
    }

    private List<String> getDeclares() {
        // 带路径的配置
        List<String> declares = action.getDeclares();
        if(declares != null) return declares;

        declares = seed.getDeclares();
        if(declares != null) return declares;

        return SeedKit.getDefaultDeclares(seed);
    }

    private List<String> getSearches() {
        // 带路径的配置
        List<String> searched = action.getSearches();
        if(searched != null) return searched;

        searched = seed.getSearches();
        if(searched != null) return searched;

        return Collections.emptyList();
    }

    private Map<String, Stat> getStats() {
        Map<String, Stat> stats = action.getStats();
        if(stats != null) return stats;

        stats = seed.getStats();
        if(stats != null) return stats;

        return Collections.emptyMap();
    }

    public StatService searchParams(Map<String, String> searchParams) {
        this.searchParams = searchParams;
        return this;
    }

    public StatService keyword(String keyword) {
        this.keyword = keyword;
        return this;
    }

    public StatService dataPermis(List<List<String>> dataPermis) {
        this.dataPermis = dataPermis;
        return this;
    }
}
